//
//  AppDelegate.m
//  PongServer
//
//  Created by Jonathan Saggau on 8/9/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "ClientController.h"
#import "ServerController.h"
#import "PongView.h"
#import "PongFieldController.h"
#import "PongPrefs.h"
#import "Common.h"

PongPrefs *sharedPongPrefs;

@implementation AppDelegate

-(void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void  *)contextInfo;
{
    exit(1);
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification 
{
    //LogMethod();
        sharedPongPrefs = [[PongPrefs alloc] init];
        [sharedPongPrefs setPort:12000];
        [sharedPongPrefs setHostName:@"192.168.1.102"];
        timer = [NSTimer scheduledTimerWithTimeInterval:ANIM_INTERVAL target:self selector:@selector(heartbeatCallback:) userInfo:nil repeats:YES];
        fieldController = [[PongFieldController alloc] initWithFieldRect:[view frame]];
        [fieldController setDelegate:self];
        [view setField:[fieldController field]];
        sleep(2);
        [self beginNewGame];
}


-(void)dealloc
{
    [fieldController setDelegate:nil];
    [timer invalidate];
    [timer release]; timer = nil;
    [fieldController release]; fieldController = nil;
	[super dealloc];
}

-(void)heartbeatCallback:(NSTimer *)aTimer
{
    [fieldController update];
    [view setNeedsDisplay:YES];
}

-(void)beginNewGame
{
    //LogMethod();
    [fieldController beginNewGame];
}

-(void)gameDidEnd
{
    //LogMethod();
    [self performSelector:@selector(beginNewGame) withObject:nil afterDelay:0.2];
    [self beginNewGame];
}

-(void)crashAndBurn;
{
    NSApplication *app = [NSApplication sharedApplication];
    [app terminate:self];
}

@end
